# To change this template, choose Tools | Templates
# and open the template in the editor.
require 'win32ole'
require 'util'

class Excel
  @@excel = nil
  def initialize(file_path)
    @file_path = file_path
    if (!@@excel)
      @@excel=WIN32OLE.new("excel.application")
      @@excel['Visible']=TRUE
    end
  end
  def open(mode)
    @workbook = @@excel.Workbooks.open @file_path
  end
  def first_sheet
    @workbook.worksheets(1)
  end
  def write(text)
    @workbook.worksheets(1).Range("a50:a500").each {|e|
      if e.value.nil?
        e.value = text
        puts "write edit in document"
        return
      end
    }
  end
  def find(find_text)
    find_range("a1:z2500", find_text)
  end
  def find_range(range, find_text)
    @workbook.worksheets(1).Range(range).find(find_text)
  end
  def match(find_or_get, conditions, results)
    if methods.include?(find_or_get)
      command = method(find_or_get).call
    else
      command = find(find_or_get)
    end
    puts "find? #{command}"
    command = command
    Util.command_send(command, conditions, results)
  end
  def check_answer(answer_lines)
    answer_count = 0
    answer_lines.each { |answer|
      if (match(answer[0], answer[1], answer[2]))
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} match"
        answer_count += 1
      else
        puts "#{answer[0]}, #{answer[1]}, #{answer[2]} not match"
      end
    }
    return answer_count
  end
  def close
    @workbook.close(true)
  end
end
